<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const time = `10小时50分钟`
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        backgroundColor: '#fff',
        title: {
          show: false,
          text: '订单统计',
          textStyle: {
            fontSize: 19
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#9dbbf4',
              width: 1
            }
          },
          formatter: '学习时间 : <br> {c}分钟'
        },
        color: ['#3f82ff'],
        legend: {
          top: 50,
          itemWidth: 28,
          itemHeight: 15,
          itemGap: 13,
          data: [`学习总时长 ${time}`],
          left: 'center',
          textStyle: {
            fontSize: 18,
            color: '#000'
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          },
          right: 20
        },
        grid: {
          top: 120,
          left: 40,
          right: '5%',
          bottom: '2%',
          containLabel: true,
          height: 350
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#e5e7ea'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            margin: 10,
            color: '#9fa0a3',
            textStyle: {
              fontSize: 14
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#e5e7ea'
            }
          },
          data: ['03-01', '03-02', '03-03', '03-04', '03-05', '03-06', '03-07']
        }],
        yAxis: [{
          type: 'value',
          name: '学习时长/分钟',
          nameGap: 55,
          nameTextStyle: {
            fontSize: 18,
            color: '#7a7a7c'
          },
          min: '0',
          max: '40',
          splitNumber: 4,
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#e5e7ea'
            }
          },
          axisLabel: {
            margin: 10,
            color: '#7a7a7c',
            textStyle: {
              fontSize: 14
            }
          },
          splitLine: {
            lineStyle: {
              color: '#e5e7ea'
            }
          }
        }],
        series: [
          {
            name: `学习总时长 ${time}`,
            type: 'line',
            smooth: true,
            // symbol: 'circle',
            symbolSize: 6,
            showSymbol: true,
            lineStyle: {
              normal: {
                width: 4,
                color: '#3f82ff'
              }
            },
            data: [12, 14, 25, 37, 26, 18, 10]
          }
        ]
      })
    }
  }
}
</script>
